package gae.testjig.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class UIFeedbackLabel extends Composite {

	interface UIFeedbackLabelUiBinder extends UiBinder<Widget, UIFeedbackLabel> {}
	private static UIFeedbackLabelUiBinder uiBinder = GWT.create(UIFeedbackLabelUiBinder.class);

	@UiField Image imgWait;
	@UiField Label lblFeedback;
	
	public interface FeedbackListener {
		public void onFeedback(String message);
	}

	private List<FeedbackListener> listeners = new ArrayList<FeedbackListener>();

	
	@UiConstructor
	public UIFeedbackLabel(String message, boolean isSpinning) {
		initWidget(uiBinder.createAndBindUi(this));
		this.spin(isSpinning);
		this.setText(message);
	}

	public UIFeedbackLabel() {
	}
	
	public void addFeedbackListener(FeedbackListener listener) {
		listeners.add(listener);
	}
	
	public void setWaitImageUrl(String url) {
		this.imgWait.setUrl(url);
	}

	public void setTextAndSpin(String message) {
		this.spin(true);
		this.setText(message);
	}

	public void setTextNoSpin(String message) {
		this.spin(false);
		this.setText(message);
	}

	public void setText(String message) {
		lblFeedback.setText(message);
		for ( FeedbackListener listener : listeners ) {
			listener.onFeedback(message);
		}
	}
	
	public void spin(boolean doSpin) {
		if ( doSpin ) {
			imgWait.setVisible(true);
		} else {
			imgWait.setVisible(false);
		}
	}


}
